/*
 * es9018.h  --  codec driver for ES9018
 *
 * Copyright (C) 2017 XiaoMi, Inc.
 *
 * Author: Xiang Xiao <xiaoxiang@xiaomi.com>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
 *
 */

#ifndef ES9018_H
#define ES9018_H

/* ES9018 register map */
#define ES9018_SYSTEM_SETTINGS				0
#define ES9018_INPUT_CONFIGURATION			1
#define ES9018_SOFT_VOLUME_CONTROL1			4
#define ES9018_SOFT_VOLUME_CONTROL2			5
#define ES9018_SOFT_VOLUME_CONTROL3			6
#define ES9018_GENERAL_SETTINGS				7
#define ES9018_GPIO_CONFIGURATION			8
#define ES9018_MASTER_MODE_CONTROL			9
#define ES9018_MASTER_MODE_CONTROL2			10
#define ES9018_CHANNEL_MAPPING				11
#define ES9018_DPLL_ASRC_SETTINGS			12
#define ES9018_THD_COMPENSATION				13
#define ES9018_SOFT_START_SETTINGS			14
#define ES9018_VOLUME1					15
#define ES9018_VOLUME2					16
#define ES9018_MASTER_TRIM				17
#define ES9018_INPUT_SELECT_CONTROL			21
#define ES9018_THD2_COMPENSATION			23
#define ES9018_THD3_COMPENSATION			25
#define ES9018_FILTER_ADDRESS				26
#define ES9018_FILTER_COEFFICIENT			27
#define ES9018_FILTER_CONTROL				30
#define ES9018_SYSTEM_STATUS				64
#define ES9018_GPIO_STATUS				65
#define ES9018_DPLL_NUMBER				66
#define ES9018_SPDIF_CHANNEL_STATUS			70
#define ES9018_REGISTER_NUMBER				94

/* ES9018_SYSTEM_SETTINGS bitfield */
#define ES9018_OSC_DRV_100				0x00
#define ES9018_OSC_DRV_75				0x80
#define ES9018_OSC_DRV_50				0xc0
#define ES9018_OSC_DRV_25				0xe0
#define ES9018_OSC_DRV_0				0xf0
#define ES9018_OSC_DRV_MSK				0xf0
#define ES9018_OSC_DRV_POS				4
#define ES9018_OSC_DRV_LEN				4
#define ES9018_OSC_DRV_MAX				15

#define ES9018_SOFT_RESET				0x01
#define ES9018_SOFT_RESET_MSK				0x01
#define ES9018_SOFT_RESET_POS				0
#define ES9018_SOFT_RESET_LEN				1
#define ES9018_SOFT_RESET_MAX				1

/* ES9018_INPUT_CONFIGURATION bitfield */
#define ES9018_I2S_LENGTH_16BIT				0x00
#define ES9018_I2S_LENGTH_24BIT				0x40
#define ES9018_I2S_LENGTH_32BIT				0x80
#define ES9018_I2S_LENGTH_MSK				0xc0
#define ES9018_I2S_LENGTH_POS				6
#define ES9018_I2S_LENGTH_LEN				2
#define ES9018_I2S_LENGTH_MAX				3

#define ES9018_I2S_MODE_I2S				0x00
#define ES9018_I2S_MODE_LJ				0x10
#define ES9018_I2S_MODE_MSK				0x30
#define ES9018_I2S_MODE_POS				4
#define ES9018_I2S_MODE_LEN				2
#define ES9018_I2S_MODE_MAX				3

#define ES9018_AUTO_INPUT_INPUT				0x00
#define ES9018_AUTO_INPUT_I2S_DSD			0x04
#define ES9018_AUTO_INPUT_I2S_SPDIF			0x08
#define ES9018_AUTO_INPUT_I2S_SPDIF_DSD			0x0c
#define ES9018_AUTO_INPUT_MSK				0x0c
#define ES9018_AUTO_INPUT_POS				2
#define ES9018_AUTO_INPUT_LEN				2
#define ES9018_AUTO_INPUT_MAX				3

#define ES9018_INPUT_I2S				0x00
#define ES9018_INPUT_SPDIF				0x01
#define ES9018_INPUT_DSD				0x03
#define ES9018_INPUT_MSK				0x03
#define ES9018_INPUT_POS				0
#define ES9018_INPUT_LEN				2
#define ES9018_INPUT_MAX				3

/* ES9018_SOFT_VOLUME_CONTROL1 bitfield */
#define ES9018_AUTOMUTE_TIME_MSK			0xff
#define ES9018_AUTOMUTE_TIME_POS			0
#define ES9018_AUTOMUTE_TIME_LEN			8
#define ES9018_AUTOMUTE_TIME_MAX			255

/* ES9018_SOFT_VOLUME_CONTROL2 bitfield */
#define ES9018_AUTOMUTE_LOOPBACK			0x80
#define ES9018_AUTOMUTE_LOOPBACK_MSK			0x80
#define ES9018_AUTOMUTE_LOOPBACK_POS			7
#define ES9018_AUTOMUTE_LOOPBACK_LEN			1
#define ES9018_AUTOMUTE_LOOPBACK_MAX			1

#define ES9018_AUTOMUTE_LEVEL_MSK			0x7f
#define ES9018_AUTOMUTE_LEVEL_POS			0
#define ES9018_AUTOMUTE_LEVEL_LEN			7
#define ES9018_AUTOMUTE_LEVEL_MAX			127

/* ES9018_SOFT_VOLUME_CONTROL3 bitfield */
#define ES9018_SPDIF_AUDIO_DEEMPH			0x80
#define ES9018_SPDIF_AUDIO_DEEMPH_MSK			0x80
#define ES9018_SPDIF_AUDIO_DEEMPH_POS			7
#define ES9018_SPDIF_AUDIO_DEEMPH_LEN			1
#define ES9018_SPDIF_AUDIO_DEEMPH_MAX			1

#define ES9018_DEEMPH_BYPASS				0x40
#define ES9018_DEEMPH_BYPASS_MSK			0x40
#define ES9018_DEEMPH_BYPASS_POS			6
#define ES9018_DEEMPH_BYPASS_LEN			1
#define ES9018_DEEMPH_BYPASS_MAX			1

#define ES9018_DEEMPH_SEL_32KHZ				0x00
#define ES9018_DEEMPH_SEL_44KHZ				0x10
#define ES9018_DEEMPH_SEL_48KHZ				0x20
#define ES9018_DEEMPH_SEL_MSK				0x30
#define ES9018_DEEMPH_SEL_POS				4
#define ES9018_DEEMPH_SEL_LEN				2
#define ES9018_DEEMPH_SEL_MAX				3

#define ES9018_VOL_RATE_MSK				0x07
#define ES9018_VOL_RATE_POS				0
#define ES9018_VOL_RATE_LEN				3
#define ES9018_VOL_RATE_MAX				7

/* ES9018_GENERAL_SETTINGS bitfield */
#define ES9018_FILTER_SHAPE_FAST_ROLLOFF		0x00
#define ES9018_FILTER_SHAPE_SLOW_ROLLOFF		0x20
#define ES9018_FILTER_SHAPE_MINIMUM_PHASE		0x40
#define ES9018_FILTER_SHAPE_MSK				0x60
#define ES9018_FILTER_SHAPE_POS				5
#define ES9018_FILTER_SHAPE_LEN				2
#define ES9018_FILTER_SHAPE_MAX				3

#define ES9018_IIR_BW_47KHZ				0x00
#define ES9018_IIR_BW_50KHZ				0x04
#define ES9018_IIR_BW_60KHZ				0x08
#define ES9018_IIR_BW_70KHZ				0x0c
#define ES9018_IIR_BW_MSK				0x0c
#define ES9018_IIR_BW_POS				2
#define ES9018_IIR_BW_LEN				2
#define ES9018_IIR_BW_MAX				3

#define ES9018_CH2_MUTE					0x02
#define ES9018_CH2_MUTE_MSK				0x02
#define ES9018_CH2_MUTE_POS				1
#define ES9018_CH2_MUTE_LEN				1
#define ES9018_CH2_MUTE_MAX				1

#define ES9018_CH1_MUTE					0x01
#define ES9018_CH1_MUTE_MSK				0x01
#define ES9018_CH1_MUTE_POS				0
#define ES9018_CH1_MUTE_LEN				1
#define ES9018_CH1_MUTE_MAX				1

/* ES9018_GPIO_CONFIGURATION bitfield */
#define ES9018_GPIO2_CFG_AUTOMUTE_STATUS		0x00
#define ES9018_GPIO2_CFG_LOCK_STATUS			0x10
#define ES9018_GPIO2_CFG_MINIMUM_VOLUME			0x20
#define ES9018_GPIO2_CFG_MCLK				0x30
#define ES9018_GPIO2_CFG_LOCK_INTERRUPT			0x40
#define ES9018_GPIO2_CFG_AUTOMUTE_INTERRUPT		0x50
#define ES9018_GPIO2_CFG_LOCK_AUTOMUTE_INTERRUPT	0x60
#define ES9018_GPIO2_CFG_OUTPUT0			0x70
#define ES9018_GPIO2_CFG_INPUT				0x80
#define ES9018_GPIO2_CFG_INPUT_I2S_SPDIF_DSD		0x90
#define ES9018_GPIO2_CFG_OUTPUT1			0xf0
#define ES9018_GPIO2_CFG_MSK				0xf0
#define ES9018_GPIO2_CFG_POS				4
#define ES9018_GPIO2_CFG_LEN				4
#define ES9018_GPIO2_CFG_MAX				15

#define ES9018_GPIO1_CFG_AUTOMUTE_STATUS		0x00
#define ES9018_GPIO1_CFG_LOCK_STATUS			0x01
#define ES9018_GPIO1_CFG_MINIMUM_VOLUME			0x02
#define ES9018_GPIO1_CFG_MCLK				0x03
#define ES9018_GPIO1_CFG_LOCK_INTERRUPT			0x04
#define ES9018_GPIO1_CFG_AUTOMUTE_INTERRUPT		0x05
#define ES9018_GPIO1_CFG_LOCK_AUTOMUTE_INTERRUPT	0x06
#define ES9018_GPIO1_CFG_OUTPUT0			0x07
#define ES9018_GPIO1_CFG_INPUT				0x08
#define ES9018_GPIO1_CFG_INPUT_I2S_SPDIF_DSD		0x09
#define ES9018_GPIO1_CFG_OUTPUT1			0x0f
#define ES9018_GPIO1_CFG_MSK				0x0f
#define ES9018_GPIO1_CFG_POS				0
#define ES9018_GPIO1_CFG_LEN				4
#define ES9018_GPIO1_CFG_MAX				15

/* ES9018_MASTER_MODE_CONTROL[2] bitfield */
#define ES9018_MASTER_CLOCK				0x80
#define ES9018_MASTER_CLOCK_MSK				0x80
#define ES9018_MASTER_CLOCK_POS				7
#define ES9018_MASTER_CLOCK_LEN				1
#define ES9018_MASTER_CLOCK_MAX				1

#define ES9018_CLOCK_DIVIDER_4				0x00
#define ES9018_CLOCK_DIVIDER_8				0x20
#define ES9018_CLOCK_DIVIDER_16				0x40
#define ES9018_CLOCK_DIVIDER_MSK			0x60
#define ES9018_CLOCK_DIVIDER_POS			5
#define ES9018_CLOCK_DIVIDER_LEN			2
#define ES9018_CLOCK_DIVIDER_MAX			3

#define ES9018_SYNC_MODE_QUICK				0x10
#define ES9018_SYNC_MODE_MSK				0x10
#define ES9018_SYNC_MODE_POS				4
#define ES9018_SYNC_MODE_LEN				1
#define ES9018_SYNC_MODE_MAX				1

#define ES9018_STOP_DIV_16384				0x00
#define ES9018_STOP_DIV_8192				0x01
#define ES9018_STOP_DIV_5461				0x02
#define ES9018_STOP_DIV_4096				0x03
#define ES9018_STOP_DIV_3276				0x04
#define ES9018_STOP_DIV_2730				0x05
#define ES9018_STOP_DIV_2340				0x06
#define ES9018_STOP_DIV_2048				0x07
#define ES9018_STOP_DIV_1820				0x08
#define ES9018_STOP_DIV_1638				0x09
#define ES9018_STOP_DIV_1489				0x0a
#define ES9018_STOP_DIV_1365				0x0b
#define ES9018_STOP_DIV_1260				0x0c
#define ES9018_STOP_DIV_1170				0x0d
#define ES9018_STOP_DIV_1092				0x0e
#define ES9018_STOP_DIV_1024				0x0f
#define ES9018_STOP_DIV_MSK				0x0f
#define ES9018_STOP_DIV_POS				0
#define ES9018_STOP_DIV_LEN				4
#define ES9018_STOP_DIV_MAX				15

/* ES9018_CHANNEL_MAPPING bitfield */
#define ES9018_SPDIF_SEL_DATA_CLK			0x00
#define ES9018_SPDIF_SEL_DATA2				0x10
#define ES9018_SPDIF_SEL_DATA1				0x20
#define ES9018_SPDIF_SEL_GPIO1				0x30
#define ES9018_SPDIF_SEL_GPIO2				0x40
#define ES9018_SPDIF_SEL_MSK				0x70
#define ES9018_SPDIF_SEL_POS				4
#define ES9018_SPDIF_SEL_LEN				3
#define ES9018_SPDIF_SEL_MAX				7

#define ES9018_CH2_ANALOG_SWAP				0x08
#define ES9018_CH2_ANALOG_SWAP_MSK			0x08
#define ES9018_CH2_ANALOG_SWAP_POS			3
#define ES9018_CH2_ANALOG_SWAP_LEN			1
#define ES9018_CH2_ANALOG_SWAP_MAX			1

#define ES9018_CH1_ANALOG_SWAP				0x04
#define ES9018_CH1_ANALOG_SWAP_MSK			0x04
#define ES9018_CH1_ANALOG_SWAP_POS			2
#define ES9018_CH1_ANALOG_SWAP_LEN			1
#define ES9018_CH1_ANALOG_SWAP_MAX			1

#define ES9018_CH2_SEL_LEFT				0x00
#define ES9018_CH2_SEL_RIGHT				0x02
#define ES9018_CH2_SEL_MSK				0x02
#define ES9018_CH2_SEL_POS				1
#define ES9018_CH2_SEL_LEN				1
#define ES9018_CH2_SEL_MAX				1

#define ES9018_CH1_SEL_LEFT				0x00
#define ES9018_CH1_SEL_RIGHT				0x01
#define ES9018_CH1_SEL_MSK				0x01
#define ES9018_CH1_SEL_POS				0
#define ES9018_CH1_SEL_LEN				1
#define ES9018_CH1_SEL_MAX				1

/* ES9018_DPLL_ASRC_SETTINGS bitfield */
#define ES9018_DPLL_BW_I2S_MSK				0xf0
#define ES9018_DPLL_BW_I2S_POS				4
#define ES9018_DPLL_BW_I2S_LEN				4
#define ES9018_DPLL_BW_I2S_MAX				15

#define ES9018_DPLL_BW_DSD_MSK				0x0f
#define ES9018_DPLL_BW_DSD_POS				0
#define ES9018_DPLL_BW_DSD_LEN				4
#define ES9018_DPLL_BW_DSD_MAX				15

/* ES9018_SOFT_START_SETTINGS bitfield */
#define ES9018_SOFT_START				0x80
#define ES9018_SOFT_START_MSK				0x80
#define ES9018_SOFT_START_POS				7
#define ES9018_SOFT_START_LEN				1
#define ES9018_SOFT_START_MAX				1

#define ES9018_SOFT_START_ON_LOCK			0x40
#define ES9018_SOFT_START_ON_LOCK_MSK			0x40
#define ES9018_SOFT_START_ON_LOCK_POS			6
#define ES9018_SOFT_START_ON_LOCK_LEN			1
#define ES9018_SOFT_START_ON_LOCK_MAX			1

#define ES9018_MUTE_ON_LOCK				0x20
#define ES9018_MUTE_ON_LOCK_MSK				0x20
#define ES9018_MUTE_ON_LOCK_POS				5
#define ES9018_MUTE_ON_LOCK_LEN				1
#define ES9018_MUTE_ON_LOCK_MAX				1

#define ES9018_SOFT_START_TIME_MSK			0x1f
#define ES9018_SOFT_START_TIME_POS			0
#define ES9018_SOFT_START_TIME_LEN			5
#define ES9018_SOFT_START_TIME_MAX			31

/* ES9018_THD_COMPENSATION bitfield */
#define ES9018_THD_COMPENSATION_MSK			0xff
#define ES9018_THD_COMPENSATION_POS			0
#define ES9018_THD_COMPENSATION_LEN			8
#define ES9018_THD_COMPENSATION_MAX			255

/* ES9018_VOLUME1 bitfield */
#define ES9018_VOLUME1_MSK				0xff
#define ES9018_VOLUME1_POS				0
#define ES9018_VOLUME1_LEN				8
#define ES9018_VOLUME1_MAX				255

/* ES9018_VOLUME2 bitfield */
#define ES9018_VOLUME2_MSK				0xff
#define ES9018_VOLUME2_POS				0
#define ES9018_VOLUME2_LEN				8
#define ES9018_VOLUME2_MAX				255

/* ES9018_INPUT_SELECT_CONTROL bitfield */
#define ES9018_GPIO_INPUT_SEL2_I2S			0x00
#define ES9018_GPIO_INPUT_SEL2_SPDIF			0x40
#define ES9018_GPIO_INPUT_SEL2_DSD			0xc0
#define ES9018_GPIO_INPUT_SEL2_MSK			0xc0
#define ES9018_GPIO_INPUT_SEL2_POS			6
#define ES9018_GPIO_INPUT_SEL2_LEN			2
#define ES9018_GPIO_INPUT_SEL2_MAX			3

#define ES9018_GPIO_INPUT_SEL1_I2S			0x00
#define ES9018_GPIO_INPUT_SEL1_SPDIF			0x10
#define ES9018_GPIO_INPUT_SEL1_DSD			0x30
#define ES9018_GPIO_INPUT_SEL1_MSK			0x30
#define ES9018_GPIO_INPUT_SEL1_POS			4
#define ES9018_GPIO_INPUT_SEL1_LEN			2
#define ES9018_GPIO_INPUT_SEL1_MAX			3

#define ES9018_BYPASS_IIR				0x04
#define ES9018_BYPASS_IIR_MSK				0x04
#define ES9018_BYPASS_IIR_POS				2
#define ES9018_BYPASS_IIR_LEN				1
#define ES9018_BYPASS_IIR_MAX				1

#define ES9018_BYPASS_OSF				0x01
#define ES9018_BYPASS_OSF_MSK				0x01
#define ES9018_BYPASS_OSF_POS				0
#define ES9018_BYPASS_OSF_LEN				1
#define ES9018_BYPASS_OSF_MAX				1

/* ES9018_THD_2ND_COMPENSATION bitfield */
#define ES9018_THD2_COMPENSATION_MSK			0xff
#define ES9018_THD2_COMPENSATION_POS			0
#define ES9018_THD2_COMPENSATION_LEN			8
#define ES9018_THD2_COMPENSATION_MAX			255

/* ES9018_THD_3RD_COMPENSATION bitfield */
#define ES9018_THD3_COMPENSATION_MSK			0xff
#define ES9018_THD3_COMPENSATION_POS			0
#define ES9018_THD3_COMPENSATION_LEN			8
#define ES9018_THD3_COMPENSATION_MAX			255

/* ES9018_FILTER_ADDRESS bitfield */
#define ES9018_PROG_COEFF_STAGE1			0x00
#define ES9018_PROG_COEFF_STAGE2			0x80
#define ES9018_PROG_COEFF_STAGE_MSK			0x80
#define ES9018_PROG_COEFF_STAGE_POS			7
#define ES9018_PROG_COEFF_STAGE_LEN			1
#define ES9018_PROG_COEFF_STAGE_MAX			1

#define ES9018_PROG_COEFF_ADDR_MSK			0x7f
#define ES9018_PROG_COEFF_ADDR_POS			0
#define ES9018_PROG_COEFF_ADDR_LEN			7
#define ES9018_PROG_COEFF_ADDR_MAX			127

/* ES9018_FILTER_CONTROL bitfield */
#define ES9018_COEFF_STAGE2_EVEN			0x04
#define ES9018_COEFF_STAGE2_EVEN_MSK			0x04
#define ES9018_COEFF_STAGE2_EVEN_POS			2
#define ES9018_COEFF_STAGE2_EVEN_LEN			1
#define ES9018_COEFF_STAGE2_EVEN_MAX			1

#define ES9018_PROG_COEFF_WRITING			0x02
#define ES9018_PROG_COEFF_WRITING_MSK			0x02
#define ES9018_PROG_COEFF_WRITING_POS			1
#define ES9018_PROG_COEFF_WRITING_LEN			1
#define ES9018_PROG_COEFF_WRITING_MAX			1

#define ES9018_PROG_COEFF_ENABLE			0x01
#define ES9018_PROG_COEFF_ENABLE_MSK			0x01
#define ES9018_PROG_COEFF_ENABLE_POS			0
#define ES9018_PROG_COEFF_ENABLE_LEN			1
#define ES9018_PROG_COEFF_ENABLE_MAX			1

/* ES9018_SYSTEM_STATUS bitfield */
#define ES9018_REVISION_MSK				0x20
#define ES9018_REVISION_POS				5
#define ES9018_REVISION_LEN				1
#define ES9018_REVISION_MAX				1

#define ES9018_CHIP_ID_MSK				0x1c
#define ES9018_CHIP_ID_POS				2
#define ES9018_CHIP_ID_LEN				3
#define ES9018_CHIP_ID_MAX				7

#define ES9018_AUTOMUTE_STATUS				0x02
#define ES9018_AUTOMUTE_STATUS_MSK			0x02
#define ES9018_AUTOMUTE_STATUS_POS			1
#define ES9018_AUTOMUTE_STATUS_LEN			1
#define ES9018_AUTOMUTE_STATUS_MAX			1

#define ES9018_LOCK_STATUS				0x01
#define ES9018_LOCK_STATUS_MSK				0x01
#define ES9018_LOCK_STATUS_POS				0
#define ES9018_LOCK_STATUS_LEN				1
#define ES9018_LOCK_STATUS_MAX				1

/* ES9018_GPIO_STATUS bitfield */
#define ES9018_GPIO2_STATUS				0x02
#define ES9018_GPIO2_STATUS_MSK				0x02
#define ES9018_GPIO2_STATUS_POS				1
#define ES9018_GPIO2_STATUS_LEN				1
#define ES9018_GPIO2_STATUS_MAX				1

#define ES9018_GPIO1_STATUS				0x01
#define ES9018_GPIO1_STATUS_MSK				0x01
#define ES9018_GPIO1_STATUS_POS				0
#define ES9018_GPIO1_STATUS_LEN				1
#define ES9018_GPIO1_STATUS_MAX				1


void es9018_set_headphone(bool insert);

#endif
